<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 1. m+n格子的最终步数方案
    // const m = 4
    // const n = 4

    // const res = [[1,1,1,1]]
    // for(let i=1; i<m; i++) {
    //   const arr = [1]
    //   for(let j = 1; j<n; j++) {
    //     const val = arr[j-1] + res[i-1][j]
    //     arr.push(val)
    //   }
    //   res.push(arr)
    // }
    // console.log(res)

    // 2. 数组转数
    let arrData = [
      { key: 1, title: '1', pid: 0, isLeaf: false, children: [] },
      { key: 11, title: '1-1', pid: 1, isLeaf: false, children: [] },
      { key: 111, title: '1-1-1', pid: 11, isLeaf: true, children: [] },
      { key: 112, title: '1-1-2', pid: 11, isLeaf: false, children: [] },
      { key: 1121, title: '1-1-2-1', pid: 112, isLeaf: true, children: [] },
      { key: 12, title: '1-2', pid: 1, isLeaf: false, children: [] },

      { key: 2, title: '2', pid: 0, isLeaf: false, children: [] },
      { key: 21, title: '2-1', pid: 2, isLeaf: true, children: [] },
      { key: 22, title: '2-2', pid: 2, isLeaf: false, children: [] },
      { key: 221, title: '2-2-1', pid: 22, isLeaf: true, children: [] },

      { key: 3, title: '3', pid: 0, isLeaf: false, children: [] },
      { key: 31, title: '3-1', pid: 3, isLeaf: true, children: [] },
      { key: 32, title: '3-2', pid: 3, isLeaf: true, children: [] }
    ]
    const parentObj = {}
    arrData.forEach(( item )=> {
      const children = parentObj[item.pid] ? parentObj[item.pid] : parentObj[item.pid] = []
      children.push(item)
    })

    arrData.forEach(( item )=> {
      const key = item.key
      if(parentObj[key]) {
        item.children = parentObj[key]
      }
    })

    console.log(parentObj[0])
  </script>
</body>
</html>